<?php
/**
 * 权限控制器
 */
namespace Admin\Controller;
use Think\Controller;
class PermissionController extends InitController {
	
	// 列表
	public function lists(){
		$permissionModel = M('Permission');
		$where = array('status'=>array('NEQ',-1));
		$total = $permissionModel->where($where)->count();
	
		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';
	
		$list = $permissionModel->where($where)
				->order('taxis desc,id asc')
				->limit($Page->firstRow.','.$Page->listRows)
				->select();
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);	
		$this->assign('pager',$show);
		$this->assign('list',$list);
		$this->display('Permission/lists');
	}
	
	
	// 添加
	public function add(){
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Permission/add');
	}
	
	// 添加操作
	public function do_add(){
		if (!IS_AJAX) {
			$this->error('非法操作');
		}
		
		if (session('uid') != C('ADMIN_ID')) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'您无权限操作','field'=>'title'));
		}
		
		$title = I('title','','strip_tags,htmlspecialchars,trim');
		if (empty($title)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'标题不能为空','field'=>'title'));
		}
		
		$permissionModel = M('Permission');
		if ($permissionModel->where(array('title'=>$title))->count()) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'标题已存在，请换一个再试','field'=>'title'));
		}
		
		$action = I('action','','strip_tags,htmlspecialchars,trim');
		if (empty($action)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作动作不能为空','field'=>'title'));
		}
		$group = I('group','','strip_tags,htmlspecialchars,trim');
		$taxis = I('taxis',100,'intval');
		$status = I('status',1,'intval');
		$description = I('description','','strip_tags,htmlspecialchars,trim');
		
		$data['title'] = $title;
		$data['action'] = $action;
		$data['taxis'] = $taxis;
		$data['status'] = $status;
		$data['createtime'] = NOW_TIME;
		$data['group'] = $group;
		$data['description'] = $description;
		
		if ($permissionModel->add($data)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败','field'=>''));
		}
	}
	
	// 编辑
	public function edit(){
		$id = I('id',0,'intval');
		if (!$id) {
			$this->error('参数错误');
		}
		$info = M('Permission')->where(array('id'=>$id))->find();
		if (empty($info)) {
			$this->error('没有找到相应记录');
		}
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		
		$this->assign('info',$info);
		$this->display('Permission/edit');
	}
	
	// 编辑操作
	public function do_edit(){
		if (!IS_AJAX) {
			$this->error('非法操作');
		}
		$id = I('id',0,'intval');
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误','field'=>''));
		}
		
		if (session('uid') != C('ADMIN_ID')) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'您无权限操作','field'=>'title'));
		}
		
		$permissionModel = M('Permission');
		
		$title = I('title','','strip_tags,htmlspecialchars,trim');
		if (empty($title)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'标题不能为空','field'=>'title'));
		}
		
		if ($permissionModel->where(array('title'=>$title,'id'=>array('NEQ',$id)))->count()) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'标题已存在，请换一个再试','field'=>'title'));
		}
		
		$action = I('action','','strip_tags,htmlspecialchars,trim');
		if (empty($action)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作动作不能为空','field'=>'title'));
		}
		$group = I('group','','strip_tags,htmlspecialchars,trim');
		$taxis = I('taxis',100,'intval');
		$status = I('status',1,'intval');
		$description = I('description','','strip_tags,htmlspecialchars,trim');
		
		$data['title'] = $title;
		$data['action'] = $action;
		$data['taxis'] = $taxis;
		$data['status'] = $status;
		$data['group'] = $group;
		$data['description'] = $description;
		
		if ($permissionModel->where(array('id'=>$id))->save($data) !== false) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败','field'=>''));
		}
	}
	
	// 删除操作
	public function del(){
		if (!IS_AJAX) {
			$this->error('非法操作');
		}
		if (session('uid') != C('ADMIN_ID')) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'您无权限操作','field'=>''));
		}
		$id = I('id',0,'intval');
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误','field'=>''));
		}
		if (M('Permission')->where(array('id'=>$id))->save(array('status'=>-1)) !== false) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败','field'=>''));
		}
	}
	
}